package com.foreveross.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.service.DDManagerService;
import com.foreveross.service.LogService;
import com.foreveross.util.AuthUtil;
import com.foreveross.vo.ExpressCompany;
import com.foreveross.vo.HrCode;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.ProductType;
import com.foreveross.vo.SysMenu;
import com.foreveross.vo.SysRole;
import com.foreveross.vo.SysUser;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;
import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;

/**
 * 数据字典 管理
 * @author wm
 *
 */
@Controller
@RequestMapping(value="/DDManagerContraller")
public class DDManagerContraller extends BaseCostroller  {
	private Logger log = Logger.getLogger(DDManagerContraller.class);
	
	@Resource
	private DDManagerService ddManagerService;
	
	@Resource
	private LogService logService;

	/**
	 * 系统固定代码查看
	 * @param request
	 * @param response
	 * @param type 表名
	 * @param typeName 字段名
	 * @param codeDesc 值
	 * @param pageSize 
	 * @param page
	 */
	@RequestMapping(method=RequestMethod.POST, value="/getCodeList")
	public void getCodeList(HttpServletRequest request,HttpServletResponse response,
			String type,String typeName,String codeDesc,String houseId,
			String rows,String page){   
		ResultVo resultVo = new ResultVo();
		try{
			int countRecord = (int)ddManagerService.getCodeListCount(type, typeName,codeDesc,houseId);
			PageVo<HrCode> pageObj = new PageVo<HrCode>(page, countRecord,rows);
			pageObj.setRows(ddManagerService.getCodeList(type,typeName,codeDesc,houseId,pageObj.getpageIndex(), pageObj.getpageSize()));
			
			resultVo.setData(pageObj);
			resultVo.setSuccess(true);
			log.info("系统固定代码 查看成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("系统固定代码 查看失败");
			log.error("系统固定代码 查看失败!",e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	@RequestMapping(method=RequestMethod.POST, value="/getCodeById")
	public void getCodeById(HttpServletRequest request,HttpServletResponse response,
			String id){   
		ResultVo resultVo = new ResultVo();
		try{
			HrCode code = ddManagerService.getCodeById(id);
			
			resultVo.setData(code);
			resultVo.setSuccess(true);
			log.info("系统固定代码 查看成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("系统固定代码 查看失败");
			log.error("系统固定代码 查看失败!",e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 固定代码绑定楼盘
	 * @param request
	 * @param response
	 * @param codeId
	 * @param houseIds
	 */
	@RequestMapping(method=RequestMethod.POST, value="/updateSysCodeHouse")
	public void updateSysCodeHouse(HttpServletRequest request,HttpServletResponse response,
			String codeId,String houseIds){   
		ResultVo resultVo = new ResultVo();
		try{
			ddManagerService.updateSysCodeHouse(codeId,houseIds);
			
			resultVo.setSuccess(true);
			log.info("系统固定代码绑定楼盘 成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("系统固定代码绑定楼盘 失败");
			log.error("系统固定代码绑定楼盘 失败!",e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/getSysCodeHouse")
	public void getSysCodeHouse(HttpServletRequest request,
			HttpServletResponse response, String codeId,String updateType) {
		List<Map<String,Object>> houseList = new ArrayList<Map<String,Object>>();
		try {
			houseList =  ddManagerService.getSysCodeHouse(codeId, updateType);
			log.info("系统固定代码楼盘关系查询 成功!");
		} catch (Exception e) {
			log.error("系统固定代码楼盘关系查询失败!", e);
		}
		CommonUtil.PrintWriterObjectWithJSON(response, houseList);
	}
	
	/**
	 * 系统固定代码更新
	 * @param request
	 * @param response
	 * @param code
	 * @param updateType
	 */
	@RequestMapping(method=RequestMethod.POST, value="/updateCode")
	public void updateCode(HttpServletRequest request,HttpServletResponse response,
			HrCode code,String updateType){    
		ResultVo resultVo = new ResultVo();
		try{
			if(Constants.UPDATE_ADD.equals(updateType)){
				code.setCreater(new SysUser(CommonUtil.getUserId(request)));
				code.setCreateTime(new Date());
				ddManagerService.saveOrUpdate(code, Constants.UPDATE_ADD);
			}else if(Constants.UPDATE_EDIT.equals(updateType)){
				code.setUpdater(new SysUser(CommonUtil.getUserId(request)));
				code.setUpdateTime(new Date());
				ddManagerService.saveOrUpdate(code, Constants.UPDATE_EDIT);
			}
			resultVo.setSuccess(true);

			log.info("系统固定代码 "+updateType+"成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("系统固定代码 "+updateType+"失败");
			log.error("系统固定代码 "+updateType+"失败!",e);
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 系统固定代码删除
	 * @param request
	 * @param response
	 * @param codeIds
	 */
	@RequestMapping(method=RequestMethod.POST, value="/deleteCodeList")
	public void deleteCodeList(HttpServletRequest request,HttpServletResponse response,
			String codeIds){    
		ResultVo resultVo = new ResultVo();
		try{
			ddManagerService.deleteCodeList(codeIds);
			resultVo.setSuccess(true);
			log.info("系统固定代码 删除成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("系统固定代码 删除失败");
			log.info("系统固定代码 删除失败!");
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 产品类型 查询
	 * @param request
	 * @param response
	 * @param productType
	 * @param pageSize
	 * @param page
	 */
	@RequestMapping(method=RequestMethod.POST, value="/getProductTypeList")
	public void getProductTypeList(HttpServletRequest request,HttpServletResponse response,
			ProductType productType,String rows,String page){   
		ResultVo resultVo = new ResultVo();
		try{
			int countRecord = (int)ddManagerService.getProductTypeListCount(productType);
			PageVo<ProductType> pageObj= new PageVo<ProductType>(page, countRecord,rows);
			pageObj.setRows(ddManagerService.getProductTypeList(productType,pageObj.getpageIndex(), pageObj.getpageSize()));
			
			resultVo.setData(pageObj);
			resultVo.setSuccess(true);
			log.info("产品类型 查询成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("产品类型 查询失败");
			log.error("产品类型 查询失败!",e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/getProductTypeId")
	public void getProductTypeId(HttpServletRequest request,HttpServletResponse response,
			String id){   
		ResultVo resultVo = new ResultVo();
		try{
			ProductType productType =ddManagerService.getProductTypeList(id);
			
			resultVo.setData(productType);
			resultVo.setSuccess(true);
			log.info("产品类型 查询成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("产品类型 查询失败");
			log.error("产品类型 查询失败!",e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 产品类型 更新
	 * @param request
	 * @param response
	 * @param productType
	 * @param updateType
	 */
	@RequestMapping(method=RequestMethod.POST, value="/updateProductType")
	public void updateProductType(HttpServletRequest request,HttpServletResponse response,
			ProductType productType,String updateType){    
		ResultVo resultVo = new ResultVo();
		try{

			ddManagerService.saveOrUpdate(productType, Constants.UPDATE_EDIT);
			resultVo.setSuccess(true);
			
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_ADD, "产品类型"+Constants.getUpdateType(updateType),
					AuthUtil.getUserid(request), 
					Constants.getUpdateType(updateType)+"产品类型[" + productType.toString()
							+ "]");
			log.info("产品类型 "+updateType+"成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("产品类型 "+updateType+"失败");
			log.error("产品类型 "+updateType+"失败!",e);
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	/**
	 * 产品类型 删除
	 * @param request
	 * @param response
	 * @param codeIds
	 */
	@RequestMapping(method=RequestMethod.POST, value="/deleteProductTypeList")
	public void deleteProductTypeList(HttpServletRequest request,HttpServletResponse response,
			String ids){    
		ResultVo resultVo = new ResultVo();
		try{
			ddManagerService.deleteProductTypeList(ids);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_ADD, "产品类型删除",
					AuthUtil.getUserid(request), 
					"系统菜单[" + ids
							+ "]");
			log.info("产品类型 删除成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("产品类型 删除失败");
			log.info("产品类型 删除失败!");
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	/**
	 * 快递公司 查询
	 * @param request
	 * @param response
	 * @param productType
	 * @param OnePageCount
	 * @param currentPage
	 */
	@RequestMapping(method=RequestMethod.POST, value="/getExpressCompanyList")
	public void getExpressCompanyList(HttpServletRequest request,HttpServletResponse response,
			String name,String rows,String page){   
		ResultVo resultVo = new ResultVo();
		try{  
			int countRecord = (int)ddManagerService.getExpressCompanyCount(name);
			PageVo<ExpressCompany> pageObj = new PageVo<ExpressCompany>(page, countRecord,rows);
			pageObj.setRows(ddManagerService.getExpressCompanyList(name,pageObj.getpageIndex(), pageObj.getpageSize()));
			
			resultVo.setData(pageObj);
			resultVo.setSuccess(true);
			log.info("快递公司 查询成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("快递公司 查询失败");
			log.error("快递公司 查询失败!",e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 快递公司 添加更新
	 * @param request
	 * @param response
	 * @param productType
	 * @param updateType
	 */
	@RequestMapping(method=RequestMethod.POST, value="/addOrUpdateExpressCompany")
	public void addOrUpdateExpressCompany(HttpServletRequest request,HttpServletResponse response,
			ExpressCompany expressCompany,String updateType){    
		ResultVo resultVo = new ResultVo();
		try{

			ddManagerService.saveOrUpdate(expressCompany, updateType);
			resultVo.setSuccess(true);
			
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_ADD, "快递公司"+Constants.getUpdateType(updateType),
					AuthUtil.getUserid(request), 
					Constants.getUpdateType(updateType)+"快递公司[" + expressCompany.toString()
							+ "]");
			log.info("快递公司 "+updateType+"成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("快递公司 "+updateType+"失败");
			log.error("快递公司 "+updateType+"失败!",e);
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	/**
	 * 快递公司 删除
	 * @param request
	 * @param response
	 * @param codeIds
	 */
	@RequestMapping(method=RequestMethod.POST, value="/deleteExpressCompanyList")
	public void deleteExpressCompanyList(HttpServletRequest request,HttpServletResponse response,
			String ids){    
		ResultVo resultVo = new ResultVo();
		try{
			ddManagerService.deleteExpressCompanyList(ids);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_ADD, "快递公司删除",
					AuthUtil.getUserid(request), 
					"快递公司[" + ids
							+ "]");
			log.info("快递公司 删除成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("快递公司 删除失败");
			log.info("快递公司 删除失败!");
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	

}
